import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/Login/Login'
import Home from './components/Home/Home'
import User from './components/User/User'
import Welcome from './components/Welcome/Welcome'
import Goods from './components/GoodsManage/Goods'
import Roles from './components/Power/Roles'
import Params from './components/GoodsManage/Params'
import Categories from './components/GoodsManage/Categories'
import Reports from './components/Reports/Reports'
import Orders from './components/Orders/Orders'
import Rights from './components/Power/Rights'
import AddGood from './components/GoodsManage/AddGood'



import Test from './components/test'

Vue.use(Router)

const router = new Router({
    routes: [
        {
            path: '/',
            redirect: '/welcome'
        },
        {
            path: '/login',
            component: Login
        },
        {
            path: '/home',
            component: Home,
            children: [

                {
                    path: '/roles',
                    component: Roles
                },
                {
                    path: '/goods',
                    component: Goods
                },
                {
                    path: '/rights',
                    component: Rights
                },
                {
                    path: '/params',
                    component: Params
                },
                {
                    path: '/categories',
                    component: Categories
                },
                {
                    path: '/orders',
                    component: Orders
                },
                {
                    path: '/reports',
                    component: Reports
                },
                {
                    path: '/goods/add',
                    component: AddGood
                },
                {
                    path: '/welcome',
                    component: Welcome
                },
                {
                    path: '/users',
                    component: User
                },
                {
                    path: '/home',
                    redirect: '/welcome'
                }
            ]
        },
        {
            path: '/test',
            component: Test
        },

    ]
})

router.beforeEach((to, from, next) => {
    // to 即将跳转的地址
    // from 从哪里来的
    // 实际要去的地方
    if (to.path === '/login') return next()
    const token = window.sessionStorage.getItem('token')
    if (!token) return next('/login')
    next()
})
export default router

